package com.gem.springbootdemo.control;

import com.gem.springbootdemo.entity.User;
import com.gem.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@SessionAttributes({"user"})
@Controller
public class LoginControl {
    @Autowired
     private UserService userService;

    @RequestMapping("/toLogin")   //@CookieValue("cookie的key")
    public String toLogin(@CookieValue("username") String username, @CookieValue("password")String password, Model model){
            if(username!=null&&!username.equals("")&&password!=null&&!password.equals("")){
                //cookie里面有值
                User user=userService.Login(username,password);
                if(user!=null){
                //放置session域
                model.addAttribute("user",user);
                return "index";
                }else{
                    //放置错误
                    model.addAttribute("error","1");
                    return "login";
                }
            }else{
                //不存在cookie ,跳转到登录页面
                return "login";
            }
    }
}
